<template>
  <div class="login_wapper" >
    <div class="login_box">
      <div class="login_avtar">
        <img src="../assets/logo.png" alt="icon" />
      </div>
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="0px" class="login_form">
        <el-form-item
          prop="username">
          <el-input prefix-icon="el-icon-user-solid" placeholder="请输入账号" v-model="loginForm.username">
          </el-input>
        </el-form-item>
        <el-form-item
            prop="password">
          <el-input prefix-icon="el-icon-search" placeholder="请输入密码" v-model="loginForm.password"></el-input>
        </el-form-item>
         <el-form-item class="btns">
            <el-button type="primary" @click="submit()">登录</el-button>
            <el-button type="info" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset() {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    submit() {
      this.$refs.loginFormRef.validate(async (validate) => {
        if (validate) {
          const body = {
            url: '/login',
            params: this.loginForm,
            method: 'POST',
            fullscreen: true
          }
          const data = await this.$send(body)
          if (data.meta.status === 200) {
            // console.log(this, this.router, this.$router)
            // 保存token
            window.sessionStorage.setItem('token', data.data.token)
            this.$router.push({
              path: '/home'
            })
            this.$message.success({
              showClose: true,
              message: `${data.meta.msg}`,
              duration: 1500
            })
          } else {
            this.$message.error({
              showClose: true,
              message: `${data.meta.msg}`,
              duration: 1500
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_wapper {
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 480px;
    height: 260px;
    background: white;
    border-radius: 3px;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .login_avtar {
      width: 80px;
      height: 80px;
      border: 1px solid #eee;
      padding: 5px;
      box-shadow: 0, 0, 8px, #ddd;
      border-radius: 50%;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        background-color: #ddd;
      }
    }
  }
  .login_form {
    position: absolute;
    top: 78px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
